package com.ykwep.web.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.ykwep.web.server.data.NewsItem;

public class QuestionServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(QuestionServlet.class
			.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String email = req.getParameter("email");
		String question = req.getParameter("question");
		
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "Answer Email " + email +"\n\nMessage\n\n" + question ;
        PrintWriter out = resp.getWriter();
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("kjellwinblad@gmail.com", "Singing Code User"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("info@singingcode.com", "Singing Code"));
            msg.setSubject("[QUESTION FROM SERVICE PAGE]");
            msg.setText(msgBody);
            Transport.send(msg);
            
            out.print("The question has succesfully been sent. We will respond to you as soon as possible.");
    
        } catch (AddressException e) {
        	out.print("Could not send message, sorry. Try our e-mail address instead " + e.getMessage());
        } catch (MessagingException e) {
        	out.print("Could not send message, sorry. Try our e-mail address instead " + e.getMessage());
        }
	}

}